import uuid from "../../config/uuid"
Component({
  options: {
    multipleSlots: true,
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value: "",
    },
    userName: {
      type: Boolean,
      value: false,
    },
    back: {
      type: Boolean,
      value: true,
    },
    user: {
      type: Boolean,
      value: true,
    },
    modal: {
      type: Boolean,
      value: false,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    pageTopHeight: wx.getWindowInfo().statusBarHeight,
    mesModal: false,
    picModal: false,
    userInfo: {
      userPicture: undefined,
      userName: undefined,
    },
    changeUserInfo: {
      userPicture: '',
      userName: '',
    },
    userPicture:undefined,
    icons:[
      'gongren',
      'baba',
      'nanbaoanbaobiao',
      'nainai',
      'gongzhu',
      'nanhai',
      'nanyisheng',
      'nanxuesheng',
      'mama',
      'nanjunren',
      'nanjingcha',
      'nanchushi',
      'shengdanlaoren',
      'nanlaoshi',
      'nvfaguan',
      'nanfaguan',
      'nvbaoanbaobiao',
      'nvxuesheng',
      'nvhai',
      'nvjunren',
      'xiaochou',
      'wangzi',
      'yeye',
      'xinlang',
      'nanyinger',
      'nvyisheng',
      'nvchushi',
      'nvjingcha',
      'xiaochounv',
      'shengdannvlang',
      'xinniang',
      'nvhushi',
      'nvlaoshi',
      'nvyinger',
      'nanhushi',
    ]
  },
  lifetimes: {
    attached: function () {
      // wx.setNavigationBarColor({
      //   frontColor: '#ffffff',
      //   backgroundColor: '#ffffff00',
      // })
      // const user = wx.getStorageSync("userInfo");
      // this.setData({
      //   userInfo: {
      //     userPicture: user.userPicture,
      //     userName: user.userName,
      //   },
      // });
    },
  },
  pageLifetimes: {
    show: function () {
      const user = wx.getStorageSync("userInfo");
      this.setData({
        userInfo: {
          userPicture: user.userPicture,
          userName: user.userName,
        },
      });
    },
  },
  observers: {
    modal(newV) {
      this.setData({
        mesModal: newV,
      });
    },
    page(){
      const user = wx.getStorageSync("userInfo");
      this.setData({
        userInfo: {
          userPicture: user.userPicture,
          userName: user.userName,
        },
      });
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    goPage() {
      this.triggerEvent("goPage");
      // wx.redirectTo({ url: "/pages/home/home" });
    },
    // 选择头像
    selectObjPicture(val) {
      this.setData({
        userPicture: val.currentTarget.dataset.name
      });
    },
    // 设置用户信息 保存
    setUserInfo() {
      const changeUserInfo = this.data.changeUserInfo;
      if(!changeUserInfo.userName || !changeUserInfo.userPicture){
        wx.showToast({
          title: '请完善信息',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      this.setData({
        userInfo: { ...changeUserInfo },
        mesModal: false,
      });
      
      wx.setStorageSync("userInfo", { ...this.data.changeUserInfo });
      this.triggerEvent("setUserInfo");
    },
    // 修改名称
    contentText(e) {
      const changeUserInfo = this.data.changeUserInfo;
      changeUserInfo.userName = e.detail.value;
      this.setData({
        changeUserInfo: changeUserInfo,
      });
    },
    // 弹窗打开回显
    mesModalOpen() {
      const user = wx.getStorageSync("userInfo");
      this.setData({
        changeUserInfo: {
          userPicture: user.userPicture,
          userName: user.userName,
        },
        userPicture:user.userPicture,
        mesModal: true,
      });
    },
    // 弹窗关闭
    mesModalClose() {
      this.setData({
        mesModal: false,
      });
    },
    // 头像打开
    picModalOpen() {
      this.setData({
        picModal: true,
      });
    },
    // 头像确定
    picModalOk(){
      this.setData({
        changeUserInfo: {
          userPicture: this.data.userPicture,
          userName: this.data.changeUserInfo.userName,
        },
        picModal: false,
      });
    },
    // 头像关闭
    picModalClose() {
      this.setData({
        picModal: false,
      });
    },
  },
});
